package com.sakura.mall.coupon.couponSpuCategoryRelation.service.impl;

import com.sakura.mall.common.response.Page;
import com.sakura.mall.coupon.couponSpuCategoryRelation.domain.entity.CouponSpuCategoryRelationEntity;
import com.sakura.mall.coupon.couponSpuCategoryRelation.domain.request.AddCouponSpuCategoryRelationRequest;
import com.sakura.mall.coupon.couponSpuCategoryRelation.domain.request.UpdateCouponSpuCategoryRelationRequest;
import com.sakura.mall.coupon.couponSpuCategoryRelation.domain.request.CouponSpuCategoryRelationPageRequest;
import com.sakura.mall.coupon.couponSpuCategoryRelation.domain.vo.CouponSpuCategoryRelationVo;
import com.sakura.mall.coupon.couponSpuCategoryRelation.mapper.CouponSpuCategoryRelationMapper;
import com.sakura.mall.coupon.couponSpuCategoryRelation.service.CouponSpuCategoryRelationService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 优惠券分类关联 service impl
 *
 * @author sakura
 */
@Service
@RequiredArgsConstructor
public class CouponSpuCategoryRelationServiceImpl extends ServiceImpl<CouponSpuCategoryRelationMapper, CouponSpuCategoryRelationEntity> implements CouponSpuCategoryRelationService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(AddCouponSpuCategoryRelationRequest request) {
        // todo 其他校验逻辑
        CouponSpuCategoryRelationEntity couponSpuCategoryRelation = request.convert(CouponSpuCategoryRelationEntity.class);
        this.save(couponSpuCategoryRelation);
    }

    @Override
    public Page<CouponSpuCategoryRelationVo> queryPage(CouponSpuCategoryRelationPageRequest request) {
        IPage<CouponSpuCategoryRelationEntity> qp = request.toPage();
        LambdaQueryWrapper<CouponSpuCategoryRelationEntity> queryWrapper = new LambdaQueryWrapper<>();
        this.page(qp, queryWrapper);
        IPage<CouponSpuCategoryRelationVo> ret = qp.convert(entity -> entity.convert(CouponSpuCategoryRelationVo.class));
        return Page.convert(ret);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateById(Long id, UpdateCouponSpuCategoryRelationRequest request) {
        CouponSpuCategoryRelationEntity entity = request.convert(CouponSpuCategoryRelationEntity.class);
        entity.setId(id);
        this.updateById(entity);
    }

}

